using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using System;


	public class RotateCamera : MonoBehaviour
	{
		[SerializeField]
		private float m_Distance = 20;
		[SerializeField]
		private float m_Speed = 10;
		private float m_Angle;
		[SerializeField]
		private float m_SmoothTime = 1;
		[SerializeField]
		private int m_UpdateInterval = 60;
		private int m_StepCount;

		private Transform[] m_Targets;
		private Transform m_Target;
		private Vector3 m_Position;
		private Vector3 m_Velocity;


        public Transform redSpaceship;
        public Transform blueSpaceship;


        [System.Serializable]
        public class PlayerInfo
        {
            public AgentSatellite Agent;
            [HideInInspector]
            public Vector3 StartingPos;
            [HideInInspector]
            public Quaternion StartingRot;
            [HideInInspector]
            public Rigidbody Rb;
        }


        //List of Agents On Platform
       // public List<PlayerInfo> AgentsList = new List<PlayerInfo>();

		private void Start()
		{
			//Debug.Log(blueSpaceship.transform.localPosition);
            Transform[] spaceships = new Transform[] { redSpaceship, blueSpaceship };
            m_Targets = spaceships;
            enabled = m_Targets.Length > 0;
		    m_Target = enabled ? m_Targets[0] : null;


            //这里改变了
            /*
            m_Targets = GetComponent<Rigidbody>().Select(x => x.transform).ToArray();
			enabled = m_Targets.Length > 0;
			m_Target = enabled ? m_Targets[0] : null;
            */
           /*
            m_Targets[0] = FindObjectsOfType<>().Select(x => x.transform).ToArray();
            m_Targets[1] = FindObjectsOfType<blueSpaceship>().Select(x => x.transform).ToArray();
            enabled = m_Targets.Length > 0;
			m_Target = enabled ? m_Targets[0] : null;
           
           /*
            m_Targets[0] = AgentsList[0];
            m_Targets[1] = AgentsList[1];
            enabled = m_Targets.Length > 0;
			m_Target = enabled ? m_Targets[0] : null;
           */
           /*
           m_Targets[0] = pursuer;
           m_Targets[1] = evader;
           enabled = m_Targets.Length > 0;
		   m_Target = enabled ? m_Targets[0] : null;
           */

		}

		private void LateUpdate()
		{
			if (++m_StepCount % m_UpdateInterval == 1)
			{
				Vector3 pos = transform.position;
				float min = (m_Target.position - pos).sqrMagnitude;
				
				for (int i = 0; i < m_Targets.Length; i++)
				{
					float d = (m_Targets[i].position - pos).sqrMagnitude;
					if (d < min)
					{
						m_Target = m_Targets[i];
						min = d;
					}
				}
			}

			m_Position = Vector3.SmoothDamp(
				m_Position,
				m_Target.position, 
				ref m_Velocity, 
				m_SmoothTime);

			m_Angle += m_Speed * Time.deltaTime;

			transform.position = m_Position
				+ Quaternion.AngleAxis(m_Angle, Vector3.up)
				* Vector3.forward * m_Distance;

			transform.LookAt(m_Position);
		}
	}